package net.sangpil.applications.project.architecture
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import net.sangpil.applications.project.architecture.Building3DViewerWithFlv.CapturePad;
	import net.sangpil.applications.project.architecture.Building3DViewerWithFlv.GetCatchPosition;
	import net.sangpil.applications.project.architecture.Building3DViewerWithFlv.GetCatchPositionEvent;
	import net.sangpil.applications.project.architecture.Building3DViewerWithFlv.UVmapWithAdditionalBitmapData;
	import net.sangpil.applications.project.architecture.Building3DViewerWithFlv.UVmapWithAdditionalBitmapDataEvent;
	import net.sangpil.applications.viewer.SkinVideo;
	import net.sangpil.applications.viewer.Viewer3D;
	import net.sangpil.applications.viewer.event.SkinVideoEvent;
	import net.sangpil.classes.events.SimpleLoaderEvent;
	import net.sangpil.classes.interactions.MouseDrag;
	import net.sangpil.classes.net.SimpleLoader;
	import net.sangpil.externals.papervision3d.utils.EasyCollada;
	
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.parsers.Collada;
	import org.papervision3d.objects.primitives.Plane;

	public class Building3DViewerWithFlv extends Sprite
	{
		private var building_bottom_bm:Bitmap;
		private var imgLoader:SimpleLoader;
		private var	labelLoader:SimpleLoader;
		private var labelClip:Bitmap;
		
		private var vid:SkinVideo;
		private var viewer:Viewer3D;
		private var liveUvMap:UVmapWithAdditionalBitmapData;
		
		private var positionSrc30:Array = new Array(
		"./imageAssets/mappingSrc_east.png",
		"./imageAssets/mappingSrc_north.png",
		"./imageAssets/mappingSrc_west.png",
		"./imageAssets/mappingSrc_south.png" 
		);
		private var positionSrc70:Array = new Array(
		"./imageAssets/mappingSrc_east_70.png",
		"./imageAssets/mappingSrc_north_70.png",
		"./imageAssets/mappingSrc_west_70.png",
		"./imageAssets/mappingSrc_south_70.png"
		);
		
		
		private var capPos30Arr:Array = [];
		private var capPos70Arr:Array = [];
		
		private var capturePadArr:Array;
		private var captureClipArr:Array;
		
		private var tempArr:Array;
		
		public function Building3DViewerWithFlv()
		{
			super();
			this.addEventListener(Event.ADDED_TO_STAGE, onStage);
		}
		
		private function onStage(e:Event):void
		{
			this.init();	
		}
		
		private function init():void
		{
			this.getCapturePosition();
			/* 남, 동, 북, 서 */
			/* test */
			this.capturePadArr = [new CapturePad(), new CapturePad(), new CapturePad(), new CapturePad()];
			
			this.capturePadArr[0].x = 170.5;
			this.capturePadArr[0].y = 0;
			this.capturePadArr[0].width = 170.5;
			this.capturePadArr[0].height = 512/2;
			
			this.capturePadArr[1].x = 0;
			this.capturePadArr[1].y = 0;
			this.capturePadArr[1].width = 170.5;
			this.capturePadArr[1].height = 512/2;
			
			this.capturePadArr[2].x = 0;
			this.capturePadArr[2].y = 512/2;
			this.capturePadArr[2].width = 170.5;
			this.capturePadArr[2].height = 512/2;
			
			this.capturePadArr[3].rotation = 180;
			this.capturePadArr[3].x = 170.5*2;
			this.capturePadArr[3].y = 512;
			this.capturePadArr[3].width = 170.5;
			this.capturePadArr[3].height = 512/2;
			
			this.captureClipArr = [new captureClip(), new captureClip(), new captureClip(), new captureClip()];
			 
		}
		
		
		private function getCapturePosition():void
		{
			for(var i:uint = 0; i < this.positionSrc30.length; i++)
			{
				var gcp30:GetCatchPosition = new GetCatchPosition();
				gcp30.load(this.positionSrc30[i], "30", i);
				gcp30.addEventListener(GetCatchPositionEvent.POSITION_LOADED, gotCapturePosition);
			}
			for(var j:uint = 0; j < this.positionSrc70.length; j++)
			{
				var gcp70:GetCatchPosition = new GetCatchPosition();
				gcp70.load(this.positionSrc70[j], "70", j);
				gcp70.addEventListener(GetCatchPositionEvent.POSITION_LOADED, gotCapturePosition);
			}
		}
		
		private var count:uint = 0;
		private function gotCapturePosition(e:GetCatchPositionEvent):void
		{
			//this.tempArr = e.positionArr;
			switch(e.mode)
			{
				case "30":
					this.capPos30Arr[e.idx] = e.positionArr;
				break;
				case "70":
					this.capPos70Arr[e.idx] = e.positionArr;
				break;
			}
			this.count++;
			if(this.count == 8)
			{
				this.setLayout();
				this.addEvent();
			}
		}
		
		private function setLayout():void
		{
			this.viewer = new Viewer3D(720,480);
			//this.viewer.x = this.vid.width;
			this.addChild(this.viewer);
			this.viewer.x = 418;
			this.viewer.y = 5;
			
			
			this.vid = new SkinVideo(408, 300);
			this.vid.x = 5;
			this.vid.y = 5;
			this.addChild(this.vid);
			
			this.imgLoader = new SimpleLoader();
			this.labelLoader = new SimpleLoader();
		}
		
		private function addEvent():void
		{
			this.vid.addEventListener(SkinVideoEvent.VIDEO_LOADED, vidLoaded);
			this.vid.addEventListener(SkinVideoEvent.VIDEO_REPLAY, uvMapScale);
			this.imgLoader.addEventListener(SimpleLoaderEvent.COMPLETED, imageLoaded);
			this.imgLoader.load("modelling_d.png");
		}
		
		/* events */
		
		/* when video loaded */
		private function vidLoaded(e:SkinVideoEvent):void
		{
			for(var i:uint = 0; i < captureClipArr.length; i++)
			{
				var cpc:captureClip = this.captureClipArr[i] as captureClip;
				var mi:MouseDrag = new MouseDrag(cpc);
				cpc.x = 102 * i;
				//cpc.capRect.width = 102;
				//cpc.capRect.height = 216;
				cpc.txt.text = i.toString();
				this.vid.addChild(cpc);
			}
						
			this.liveUvMap = new UVmapWithAdditionalBitmapData();
			this.liveUvMap.addEventListener(UVmapWithAdditionalBitmapDataEvent.READY_TO_SCALE, onUVmapScale);
			this.liveUvMap.load("modelling_up.png");
			
			this.mmm = new Bitmap();
			this.addChild(this.mmm);
		}
		
		private function uvMapScale(e:SkinVideoEvent = null):void
		{
			con.x = 5;
			con.y = this.vid.height+10;
			con.width = 408;
			con.height = this.viewer.height - this.vid.height - 5;
		}
		
		/* 라이브 생겼을 때. */
		private var con:Sprite;
		private function onUVmapScale(e:UVmapWithAdditionalBitmapDataEvent):void
		{
			//add capturepad on liveUvMap
			for(var i:uint = 0; i < this.capturePadArr.length; i++)
			{	
				this.liveUvMap.addChild(this.capturePadArr[i]);
			}
			
			//add mapping source viewer.
			con = new Sprite();
			con.addChild(this.liveUvMap);
			this.uvMapScale();
			this.addChild(con);	
			
			var srclabel:SrcBtnClip = new SrcBtnClip();
			srclabel.txt.text = "Label";
			this.addChild(srclabel);
			srclabel.x = this.vid.width - srclabel.width;
			srclabel.y = this.viewer.height - (srclabel.height*2+3);
			srclabel.addEventListener(MouseEvent.MOUSE_DOWN, showLabel);
			
			var src70:SrcBtnClip = new SrcBtnClip();
			src70.txt.text = "700x700";
			src70.name = "src70";
			this.addChild(src70);
			src70.x = this.vid.width - src70.width;
			src70.y = this.viewer.height - (src70.height);
			src70.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			
			
			//add building up part to 3d viewer
			trace("collada loaded");
			//this.liveUvMap.alpha = 0.5;
			this.liveUvMap.bitmap.alpha = 0.7;
			var col:Collada = EasyCollada.movie("model_up.dae", "modelling_up_png", this.liveUvMap);
			this.viewer.basicObject.addChild(col);
			var temp:Plane = new Plane(null, 700, 700, 8, 8);;
			temp.rotationX = 90;
			temp.y = -237;
			this.viewer.basicObject.addChild(temp);
			
			this.addEventListener(Event.ENTER_FRAME, onEnter);
		}
		
		/* when bottom uv map loaded */
		private function imageLoaded(e:SimpleLoaderEvent):void
		{
			/* add bottom building to scene.basicObject */
			this.building_bottom_bm = e.content as Bitmap;
			var mtl:MaterialsList = new MaterialsList();
			var bmt:BitmapMaterial = new BitmapMaterial(this.building_bottom_bm.bitmapData, true);
			mtl.addMaterial(bmt, "modelling_down_png");
			var building_bottom:Collada = new Collada("model_down.dae", mtl);
			this.viewer.basicObject.addChild(building_bottom);
			
			/* add label */
			this.labelLoader.addEventListener(SimpleLoaderEvent.COMPLETED, labelLoaded);
			this.labelLoader.load("showLabelImg.png");
		}
		
		private function labelLoaded(e:SimpleLoaderEvent):void
		{
			trace(e.content);
			this.labelClip = e.content as Bitmap;
		}
		
		private var onf:Boolean = false;
		private function onDown(e:MouseEvent):void
		{
			trace(e.target.name);
			if(this.onf)
			{
				this.onf = false;
				e.currentTarget.txt.text = "700x700";
			}else{
				this.onf = true;
				e.currentTarget.txt.text = "350x350";
			}
		}
		
		private var lbOf:Boolean = false;
		private function showLabel(e:MouseEvent):void
		{
			if(lbOf)
			{
				this.liveUvMap.removeChild(this.labelClip);
				lbOf = false;
			}else{
				this.liveUvMap.addChild(this.labelClip);
				lbOf = true;
			}
		}
		
		private var mmm:Bitmap;
		private function onEnter(e:Event):void
		{
			var vidBMD:BitmapData = this.vid.bitmapData;
			//vidBMD.width
			//this.mmm.bitmapData = vidBMD;
			for(var j:uint = 0; j<this.capturePadArr.length; j++)
			{
				var cp:CapturePad = this.capturePadArr[j] as CapturePad;
				cp.reset();
				//trace(j, this.captureClipArr[j].x, this.captureClipArr[j].y);
				if(this.onf)
				{
					for(var i:uint = 0; i < this.capPos30Arr[j].length; i++)
					{
						var point:Point = this.capPos30Arr[j][i] as Point;
						var col:uint = vidBMD.getPixel32(point.x+this.captureClipArr[j].x, point.y+this.captureClipArr[j].y);
						cp.setPixel(point, col);
					}
				}else{
					for(var k:uint = 0; k < this.capPos70Arr[j].length; k++)
					{
						var point2:Point = this.capPos70Arr[j][k] as Point;
						var col2:uint = vidBMD.getPixel32(point2.x+this.captureClipArr[j].x, point2.y+this.captureClipArr[j].y);
						cp.setPixel(point2, col2);
					}
				}
			}
			this.viewer.singleRender();
		}
	}
}